#include <stdarg.h>
#include <stdio.h>
#include "at32f403a_407_board.h"
#include "securec.h"
#include "mySysCfg.h"
#include "los_debug.h"
#include "los_interrupt.h"


#define BUFSIZE  256

static void dputs(char const *s, int (*pFputc)(int n, FILE *cookie), void *cookie)
{
    unsigned int intSave;

    intSave = LOS_IntLock();
    while (*s) {
        pFputc(*s++, cookie);
    }
    LOS_IntRestore(intSave);
}

int hal_trace_printf(uint32_t attr, const char *fmt, ...)
{
#if SYSPRINT_ONOFF == 1
    char buf[1024] = { 0 };
    va_list ap;
    va_start(ap, fmt);
    int len = vsnprintf_s(buf, sizeof(buf), 1024 - 1, fmt, ap);
    if (len > 0) {
        dputs(buf, fputc, 0);
    } else {
        //dputs("printf error 1!\n", fputc, 0);
        printf("%s\n",fmt);
    }
    va_end(ap);
    return len;
#else
	return 0;
#endif
}

int printf(char const  *fmt, ...)
{
#if SYSPRINT_ONOFF == 1
    //char buf[BUFSIZE] = { 0 };
    char buf[BUFSIZE];  // 无需初始化，加快处理速度
    int len;
    va_list ap;
    va_start(ap, fmt);
    len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, fmt, ap);
    //len = vsprintf(buf, __format, ap);
	//len = vsprintf_s(buf,  BUFSIZE - 1,  __format,  ap);
    va_end(ap);
    if(len > 0) {
        dputs(buf, fputc, 0);
    } else {
        dputs("printf error!\n", fputc, 0);
    }

    return len;
#else
	//dputs("printf off!\n", fputc, 0);
	return 0;
#endif
}
